<template>
  <div class="login-container">
    <div class="coffee-bg"></div>
    <el-card class="login-card">
      <div slot="header">
        <h2>☕ 咖啡店管理系统</h2>
      </div>
      <el-form :model="loginForm" :rules="rules" ref="loginFormRef" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username" prefix-icon="el-icon-user" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="loginForm.password" type="password" prefix-icon="el-icon-lock" />
        </el-form-item>
        <el-form-item label="角色" prop="role">
          <el-select v-model="loginForm.role" placeholder="请选择角色" style="width: 100%">
            <el-option label="管理员" value="guanliyuan" />
            <el-option label="店长" value="dianzhang" />
            <el-option label="收银员" value="shouyinyuan" />
            <el-option label="咖啡师" value="kafeishi" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleLogin" style="width: 100%">登录</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="text" @click="$router.push('/register')">没有账号?立即注册</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { roles } from '../config/permissions'

export default {
  data() {
    return {
      loginForm: {
        username: '',
        password: '',
        role: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        role: [
          { required: true, message: '请选择角色', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    handleLogin() {
      this.$refs.loginFormRef.validate(valid => {
        if (!valid) return
        
        // 这里应该调用后端API进行验证
        // 目前使用模拟数据
        const mockUsers = {
          guanliyuan: { password: 'guanliyuan', role: 'guanliyuan' },
          dianzhang: { password: 'dianzhang', role: 'dianzhang' },
          shouyinyuan: { password: 'shouyinyuan', role: 'shouyinyuan' },
          kafeishi: { password: 'kafeishi', role: 'kafeishi' }
        }
        
        const user = mockUsers[this.loginForm.role]
        if (user && user.password === this.loginForm.password && user.role === this.loginForm.role) {
          // 登录成功
          localStorage.setItem('isLogin', '1')
          localStorage.setItem('userRole', this.loginForm.role)
          localStorage.setItem('adminUser', JSON.stringify({
            username: this.loginForm.username,
            role: this.loginForm.role,
            roleName: roles[this.loginForm.role]?.name || ''
          }))
          
          this.$message.success('登录成功')
          this.$router.push('/')
        } else {
          this.$message.error('用户名或密码错误')
        }
      })
    }
  }
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #e7d2ba 0%, #b08968 100%);
  position: relative;
  overflow: hidden;
}
.coffee-bg {
  position: absolute;
  left: 0; right: 0; top: 0; bottom: 0;
  z-index: 0;
  pointer-events: none;
  background: url('https://cdn.jsdelivr.net/gh/twitter/twemoji@14.0.2/assets/72x72/2615.png') repeat 0 0/120px auto;
  opacity: 0.08;
}
.login-card {
  width: 400px;
  border-radius: 18px;
  box-shadow: 0 8px 32px 0 rgba(80, 60, 40, 0.18);
  border: 1px solid #e0c3a0;
  background: rgba(255, 255, 255, 0.95);
  z-index: 1;
}
.login-card :deep(.el-card__header) {
  text-align: center;
  background: none;
  border-bottom: none;
  padding-bottom: 0;
}
.login-card h2 {
  margin: 0;
  color: #7c4f22;
  font-family: 'Segoe Script', 'Comic Sans MS', cursive, sans-serif;
  letter-spacing: 2px;
  font-size: 2em;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}
.el-form-item__label {
  color: #7c4f22;
  font-weight: bold;
}
.el-input__inner, .el-select {
  background: #f8f5f2;
  border-radius: 8px;
  border: 1px solid #e0c3a0;
}
.el-button--primary {
  background: linear-gradient(90deg, #a47149 0%, #7c4f22 100%);
  border: none;
  color: #fff;
  font-weight: bold;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(124, 79, 34, 0.08);
  transition: background 0.2s;
}
.el-button--primary:hover {
  background: linear-gradient(90deg, #7c4f22 0%, #a47149 100%);
}
.el-button--text {
  color: #a47149;
}
</style> 